package com.sl.plugin.step.file;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sl.core.engine.annotations.Bean;
import com.sl.core.engine.meta.row.ProcessRowDescriptor;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import com.sl.dev.api.DevFileApi;
import lombok.SneakyThrows;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.IValueMeta;
import org.apache.hop.core.row.RowMeta;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ExcelOutputComp extends AbstractProcessComp<List<Map<String, Object>>, Object> implements ProcessCompService {

    @Bean
    private DevFileApi devFileApi;


    public ExcelOutputComp(ExcelOutputCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @SneakyThrows
    @Override
    public Object input(List<Map<String, Object>> param) {
//        File excelFile = createExcelFile();

        ExcelOutputCompDescriptor descriptor = getDescriptor();
        String fileName = descriptor.getStrAttr("fileName");

        IRowMeta inputRowMeta = getInputRowMeta();
        List<IValueMeta> valueMetaList = inputRowMeta.getValueMetaList();
        ExcelWriter writer = ExcelUtil.getWriter();
        //组装rows数据
        List<Map<String, Object>> rows = Lists.newArrayList();
        param.forEach(apply -> {
            Map<String, Object> map = new HashMap<>();
            for (IValueMeta field : valueMetaList) {
                Object o = apply.get(field.getName());
                map.put(StrUtil.blankToDefault(field.getComments(), field.getName()), o);
            }
            rows.add(map);
        });

        for (IValueMeta field : valueMetaList) {
            String value = StrUtil.blankToDefault(field.getComments(), field.getName());
            writer.addHeaderAlias(value, value);
        }

        //构建表头
        //设置单元格大小
        writer.setColumnWidth(-1, 15);
        //设置单个单元格样式
        writer.write(rows, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.flush(byteArrayOutputStream, true);
        writer.close();
        byteArrayOutputStream.flush();

        String s = devFileApi.uploadReturnUrl(fileName, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));

        Map<String, Object> result = Maps.newHashMap();
        result.put("downloadPath", s);
        return result;
    }
}
